package org.mtdownload.dialogs.preferences;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.mtdownload.utils.PreferencesManager;

public class GeneralPreferencesPanel extends PreferencePanel {
	private static final long serialVersionUID = 3258135756064175410L;

	private JTextField defaultDownloadDir=null;	
	private JSpinner defaultNrOfThreads=null;
	private JTextField downloadDatabaseFile=null;
	
	public GeneralPreferencesPanel() {
		super("General");
		init();
	}
	
	private void init() {
		setLayout(new GridBagLayout());
		defaultDownloadDir=new JTextField(PreferencesManager.getDefaultSaveDir());	
		defaultNrOfThreads=new JSpinner(new SpinnerNumberModel(PreferencesManager.getDefaultThreadsNumber(),1,100,1));
		downloadDatabaseFile=new JTextField(PreferencesManager.getDownloadListDatabase());
		JLabel defaultDldDirLabel=new JLabel("Default download dir:");		
		JLabel defaultNrOfThreadsLabel=new JLabel("Default nr. of threads:");
		JLabel databaseFileLabel=new JLabel("Downloads database file:");
		defaultDldDirLabel.setDisplayedMnemonic('D');
		defaultNrOfThreadsLabel.setDisplayedMnemonic('n');
		databaseFileLabel.setDisplayedMnemonic('a');
		defaultDldDirLabel.setLabelFor(defaultDownloadDir);
		defaultNrOfThreadsLabel.setLabelFor(defaultNrOfThreads);
		databaseFileLabel.setLabelFor(downloadDatabaseFile);
		JButton dirBrowseButton=new JButton("Browse");
		dirBrowseButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				browseDir();
			}
		});
		JButton fileBrowseButton=new JButton("Browse");
		fileBrowseButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				browseFile();
			}
		});
		
		add(defaultDldDirLabel,new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));
		add(defaultDownloadDir,new GridBagConstraints(1,0,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(10,10,0,0),0,0));
		add(dirBrowseButton,new GridBagConstraints(2,0,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));

		add(defaultNrOfThreadsLabel,new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));
		add(defaultNrOfThreads,new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));
		
		add(databaseFileLabel,new GridBagConstraints(0,2,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));
		add(downloadDatabaseFile,new GridBagConstraints(1,2,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(10,10,0,0),0,0));
		add(fileBrowseButton,new GridBagConstraints(2,2,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));		
		add(new JPanel(),new GridBagConstraints(0,3,3,1,1,1,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(10,10,0,0),0,0));	
		
		defaultDownloadDir.getDocument().addDocumentListener(new DocumentListener(){
			public void insertUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
			public void removeUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
			public void changedUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
		});
		downloadDatabaseFile.getDocument().addDocumentListener(new DocumentListener(){
			public void insertUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
			public void removeUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
			public void changedUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
		});
		defaultNrOfThreads.getModel().addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				getPreferencesListener().preferencesChanged();
			}
		});		
	}

	private void browseDir(){		
		JFileChooser chooser=new JFileChooser(defaultDownloadDir.getText());
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result=chooser.showOpenDialog(this);
		if(result==JFileChooser.APPROVE_OPTION){
			defaultDownloadDir.setText(chooser.getSelectedFile().getAbsolutePath());
		}
	}
	private void browseFile(){
		JFileChooser chooser=new JFileChooser(downloadDatabaseFile.getText());
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int result=chooser.showOpenDialog(this);
		if(result==JFileChooser.APPROVE_OPTION){
			downloadDatabaseFile.setText(chooser.getSelectedFile().getAbsolutePath());
		}		
	}
	
	@Override
	public void applyChanges() {
		PreferencesManager.setDefaultSaveDir(defaultDownloadDir.getText());
		PreferencesManager.setDefaultThreadsNumber(((Integer)defaultNrOfThreads.getValue()).intValue());
		PreferencesManager.setDownloadListDatabase(downloadDatabaseFile.getText());
	}

}
